package com.hongshan.wallet.widget

import android.animation.ObjectAnimator
import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.animation.Animation
import android.view.animation.LinearInterpolator
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.hongshan.wallet.R
import kotlin.properties.Delegates

/**
 * Copyright Zhangxx.
 * Copyright (c) 2019
 * All rights reserved
 *
 * @project  : Wallet
 * @package  : com.hongshan.wallet.widget
 * @author   : zhangxx
 * @date     : 2019/5/9
 * @email    : qq957413212@hotmail.com
 * @desc     : //TODO
 */
class ProgressView(context: Context, attrs: AttributeSet?, defStyleAttr: Int):FrameLayout(context,attrs,defStyleAttr) {

    constructor(context: Context, attrs: AttributeSet?):this(context,attrs,0)
    constructor(context: Context):this(context,null)

    private val mTextView: TextView
    private var mImageView by Delegates.notNull<ImageView>()
    var llContentBg:LinearLayout by Delegates.notNull()
    val animation by lazy { ObjectAnimator.ofFloat(mImageView, "rotation", 0f, 359f) }

    init {
        LayoutInflater.from(context).inflate(R.layout.view_global_loading_status, this, true)
        mImageView = findViewById(R.id.image)
        mTextView = findViewById(R.id.text)
        llContentBg = findViewById(R.id.llContentBg)
        setBackgroundColor(Color.TRANSPARENT)
        animation.duration = 2000
        animation.interpolator = LinearInterpolator()
        animation.repeatCount = Animation.INFINITE
    }

    override fun onWindowVisibilityChanged(visibility: Int) {
        super.onWindowVisibilityChanged(visibility)
        if (visibility == View.VISIBLE){
            animation.start()
        } else {
            animation.cancel()
        }
    }
}